package com.moli.iov.down.command.design.factory;

import com.moli.iov.down.enums.command.CommandEnum;
import com.moli.iov.down.command.codec.encode.data.*;
import com.moli.iov.exception.BusinessException;
import com.moli.iov.util.ByteUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  数据编码对象工厂
 * </p>
 *
 * @author: zql
 * @Date: 2018/12/28 10:13
 */
public class DataEncoderFactory {
    private static  final Logger logger = LoggerFactory.getLogger(DataEncoderFactory.class);

    private static final Map<Byte, DataEncoder> DATA_ENCODER_MAP = new HashMap<>();

    static{
        DATA_ENCODER_MAP.put(CommandEnum.REMOTE_CONTROL.getFlag(), new ControlDataEncoder());
        DATA_ENCODER_MAP.put(CommandEnum.TERMINAL_PARAMETER_QUERY.getFlag(), new ParameterQueryDataEncoder());
        DATA_ENCODER_MAP.put(CommandEnum.TERMINAL_PARAMETER_SETTING.getFlag(), new ParameterSettingDataEncoder());
        DATA_ENCODER_MAP.put(CommandEnum.TERMINAL_CONTROL.getFlag(), new TerminalControlDataEncoder());
        DATA_ENCODER_MAP.put(CommandEnum.REMOTE_ROLL_CALL.getFlag(), new RollCallDataEncoder());
        DATA_ENCODER_MAP.put(CommandEnum.REMOTE_UPGRADE.getFlag(), new UpgradeEncoder());
    }

    private DataEncoderFactory(){}

    public static DataEncoder getDataEncoder(Byte command){
        DataEncoder dataEncoder = DATA_ENCODER_MAP.get(command);
        if(dataEncoder == null){
            String commandStr = ByteUtils.toHexString(command);
            logger.error("获取数据编码对象失败 命令: {}", commandStr);
            throw new BusinessException("获取数据编码对象失败");
        }
        return dataEncoder;
    }
}
